<template>
    <div class="list">
        <div class="table" v-if="active==1">
            <el-form :model="params" style="width: 100%;">
                <el-container>
                    <!--内容-->
                    <el-form-item>
                        <el-input v-model="params.content" style="width: 200px;"></el-input>
                    </el-form-item>
                    <!--字段类型-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="params.field" :value="params.field" style="width: 200px;">
                            <el-option
                                    v-for="type in typeList"
                                    :label="type.value"
                                    :value="type.key"
                                    :key="type.key"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item style="margin-left: 10px">
                        <el-button type="primary" @click="handleSearch">检索</el-button>
                        <el-button @click="handleAdd">添加</el-button>
                    </el-form-item>
                </el-container>
            </el-form>

            <el-table :border="true" :data="pageResult.records">
                <el-table-column prop="id" v-if="false"></el-table-column>
                <el-table-column prop="caseid" v-if="false"></el-table-column>
                <el-table-column label="案号" prop="caseNo" width="150px"></el-table-column>
                <el-table-column label="办案机关" prop="bnjg"></el-table-column>
                <el-table-column label="承办人" prop="cbr"></el-table-column>
                <el-table-column label="联系电话" prop="tel"></el-table-column>
                <el-table-column label="审查起诉时间" prop="qssjb"></el-table-column>
                <el-table-column label="审查起诉结束时间" prop="qssje"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown trigger="click" @command="handleCommandClick">
                            <el-button>
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'show')">查看</el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'update')">修改</el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'delete')">删除</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>

            <el-footer class="case-list-footer">
                <el-pagination
                        style="margin: 10px auto"
                        background
                        layout="prev, pager, next"
                        :total="pageResult.total"
                        :page-size="pageResult.size"
                        class="pagination"
                        :current-page="pageResult.current"
                        @current-change="handleIndexChange"
                >
                </el-pagination>
            </el-footer>
        </div>

        <el-dialog
                title="详细信息"
                :visible.sync="centerDialogVisible"
                width="70%"
                center>
            <jc-details :jc="jc"></jc-details>
        </el-dialog>

        <div style="width: 100%;" v-if="active==2">
            <jc-edit
                    :jc="jc"
                    :status="status"
                    @back="handleBack"
                    @update="handleUpdateSubmit"
                    @add="handleAddSubmit">
            </jc-edit>
        </div>
    </div>
</template>

<script>
    //引入组件
    const JcDetails = () => import('./JcDetails.vue')
    const JcEdit = () => import('./JcEdit.vue')
    //引入服务类
    import {JcService} from "../../../model/gs/JcService.js";

    const jcService = JcService.getInstance()

    export default {
        data() {
            return {
                centerDialogVisible:false,
                status: 'add',
                active: 1,
                params: {
                    status: '',
                    field: 'case_no'
                },
                pageResult: {},
                //搜索类型
                typeList: [
                    {
                        key: 'case_no',
                        value: '案号搜索'
                    },
                    {
                        key: 'bnjg',
                        value: '办理机关搜索'
                    },
                    {
                        key: 'cbr',
                        value: '承办人搜索'
                    }
                ],
                jc: {}
            }
        },
        methods: {
            getJcList() {
                jcService.getByPage(this.params).then(response => {
                    if (response.data.code === 0) {
                        this.pageResult = response.data.data
                    }
                })
            },
            handleSearch() {
                this.params.page = 1
                this.getJcList()
            },
            //处理角标变化的方法
            handleCommandClick(command) {
                switch (command.type) {
                    case 'show':
                        this.handleShowDetails(command.jc)
                        break
                    case 'update':
                        this.handleUpdate(command.jc)
                        break
                    case 'delete':
                        this.handleDelete(command.jc)
                        break
                }
            },
            handleDelete(jc){
                jcService.removeById(jc.id).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'删除成功',
                            duration:1000
                        })
                        //刷新数据
                        this.getJcList()
                    }
                })
            },
            //查看详情的方法
            handleShowDetails(jc) {
                this.jc = jc
                this.centerDialogVisible = true
            },
            //角标发生变化之前的操作
            beforeHandlerCommand(jc, type) {
                return {
                    jc, type
                }
            },
            handleIndexChange(index) {
                this.params.page = index
                this.getJcList()
            },
            handleUpdate(jc) {
                this.status = 'update'
                jcService.getById(jc.id).then(response => {
                    if(response.data.code === 0){
                        this.jc = response.data.data
                        this.active = 2
                    }
                })
            },
            handleUpdateSubmit() {
                //校验
                if (!jcService.check(this.jc)){
                    return
                }
                //发送请求
                jcService.update(this.jc).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'修改成功',
                            duration:1000
                        })
                        //刷新数据
                        this.handleSearch()
                        //重置jc对象
                        this.handleBack()
                    }
                })
            },
            handleAdd() {
                this.status = 'add'
                this.active = 2
            },
            handleAddSubmit() {
                //校验
                if (!jcService.check(this.jc)){
                    return
                }
                //发送请求
                jcService.save(this.jc).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'添加成功',
                            duration:1000
                        })
                        //刷新数据
                        this.handleSearch()
                        //重置jc对象
                        this.handleBack()
                    }
                })
            },
            handleBack() {
                this.jc = {}
                this.active = 1
            }
        },
        created() {
            this.getJcList()
        },
        components: {
            JcDetails,
            JcEdit
        }
    }
</script>

<style scoped>
    .list {
        width: 100%;
    }

    .case-list-footer {
        width: 100%;
        height: 10%;
        position: relative;
    }

    .case-list-footer .pagination {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
    }

    .table {
        width: 100%;
    }
</style>